package com.kch.web.controller.system;

import com.kch.common.annotation.Log;
import com.kch.common.core.controller.BaseController;
import com.kch.common.core.domain.AjaxResult;
import com.kch.common.core.page.TableDataInfo;
import com.kch.common.enums.BusinessType;
import com.kch.common.utils.StringUtils;
import com.kch.framework.util.ShiroUtils;
import com.kch.system.domain.SysTag;
import com.kch.system.service.ISysCloumnService;
import com.kch.system.service.ISysTagService;
import com.kch.system.vo.SysTagVO;
import com.kch.web.config.CloumnProperties;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 数据字典信息
 *
 */
@Controller
@RequestMapping("/system/tag")
public class SysTagController extends BaseController {
    private String prefix = "system/dict/type";

    @Autowired
    private ISysTagService sysTagService;
    @Autowired
    private ISysCloumnService sysCloumnService;
    @Autowired
    private CloumnProperties cloumnProperties;

    @RequiresPermissions("system:tag:view")
    @GetMapping()
    public String dictType(ModelMap mmap) {
        mmap.put("cols", sysCloumnService.selectSysCloumnList());
        return prefix + "/tag";
    }

    @PostMapping("/list")
    @RequiresPermissions("system:dict:list")
    @ResponseBody
    public TableDataInfo list(SysTag dictType) {
        startPage();
        List<SysTagVO> list = sysTagService.selectTagList(dictType);
        return getDataTable(list);
    }


    /**
     * 新增字典类型
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        mmap.put("cols", sysCloumnService.selectSysCloumnList());
        return prefix + "/add";
    }

    /**
     * 新增保存字典类型
     */
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    @RequiresPermissions("system:dict:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated SysTag dict) {
        if (StringUtils.isEmpty(dict.getCloumnCode())) {
            return AjaxResult.error("栏目编码不能为空");
        }
        if (ObjectUtils.isEmpty(sysCloumnService.selectByCode(dict.getCloumnCode()))) {
            return AjaxResult.error("栏目编码有误");
        }
        dict.setCreateBy(ShiroUtils.getLoginName());
        SysTag tag = new SysTag();
        tag.setTagName(dict.getTagName());
        if(sysTagService.selectByNameAndType(tag) > 0){
            throw new RuntimeException("标签名称重复,无法添加");
        }
        return toAjax(sysTagService.insertSysTag(dict));
    }

    /**
     * 修改字典类型
     */
    @GetMapping("/edit/{dictId}")
    public String edit(@PathVariable("dictId") Long dictId, ModelMap mmap) {
        mmap.put("dict", sysTagService.selectTagById(dictId));
        mmap.put("cols", sysCloumnService.selectSysCloumnList());
        return prefix + "/edit";
    }

    /**
     * 修改保存字典类型
     */
    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:dict:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated SysTag dict) {
        if (StringUtils.isEmpty(dict.getCloumnCode())) {
            return AjaxResult.error("栏目编码不能为空");
        }
        if (ObjectUtils.isEmpty(sysCloumnService.selectByCode(dict.getCloumnCode()))) {
            return AjaxResult.error("栏目编码有误");
        }
        dict.setUpdateBy(ShiroUtils.getLoginName());
        SysTag tag = new SysTag();
        tag.setTagName(dict.getTagName());
        tag.setId(dict.getId());
        if(sysTagService.selectByNameAndType(tag) > 0){
            throw new RuntimeException("要修改的标签名称已经存在！");
        }
        return toAjax(sysTagService.updateTag(dict));
    }


    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @RequiresPermissions("system:dict:remove")
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        try {
            return toAjax(sysTagService.deleteTagIds(ids));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }


    /**
     * 新增字典类型
     */
    @GetMapping("/search/add")
    public String searchAdd(ModelMap mmap) {
        List<String> list = Stream.of(cloumnProperties.getTepro(), cloumnProperties.getTeexp(),
                cloumnProperties.getTedem(), cloumnProperties.getTeque()).collect(Collectors.toList());
        mmap.put("cols", sysCloumnService.selectByCodeList(list));
        return prefix + "/search/add";
    }

    /**
     * 新增保存字典类型
     */
    @Log(title = "标签", businessType = BusinessType.INSERT)
    @PostMapping("/search/add")
    @ResponseBody
    public AjaxResult searchAddSave(@Validated SysTag dict) {
        if (StringUtils.isEmpty(dict.getCloumnCode())) {
            return AjaxResult.error("栏目编码不能为空");
        }
        if(ObjectUtils.isEmpty(dict.getType())){
            return AjaxResult.error("类型不能为空");
        }
        if (ObjectUtils.isEmpty(sysCloumnService.selectByCode(dict.getCloumnCode()))) {
            return AjaxResult.error("栏目编码有误");
        }
        dict.setCreateBy(ShiroUtils.getLoginName());
        SysTag tag = new SysTag();
        tag.setTagName(dict.getTagName());
        tag.setType(dict.getType());
        if(sysTagService.selectByNameAndType(tag) > 0){
            throw new RuntimeException("标签名称重复,无法添加");
        }
        return toAjax(sysTagService.insertSysTag(dict));
    }

    /**
     * 修改字典类型
     */
    @GetMapping("/search/edit/{dictId}")
    public String searchEdit(@PathVariable("dictId") Long dictId, ModelMap mmap) {
        mmap.put("dict", sysTagService.selectTagById(dictId));
        List<String> list = Stream.of(cloumnProperties.getTepro(), cloumnProperties.getTeexp(),
                cloumnProperties.getTedem(), cloumnProperties.getTeque()).collect(Collectors.toList());
        mmap.put("cols", sysCloumnService.selectByCodeList(list));
        return prefix + "/search/edit";
    }

    /**
     * 修改保存字典类型
     */
    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:dict:edit")
    @PostMapping("/search/edit")
    @ResponseBody
    public AjaxResult searchEditSave(@Validated SysTag dict) {
        if (StringUtils.isEmpty(dict.getCloumnCode())) {
            return AjaxResult.error("栏目编码不能为空");
        }
        if(ObjectUtils.isEmpty(dict.getType())){
            return AjaxResult.error("类型不能为空");
        }
        if (ObjectUtils.isEmpty(sysCloumnService.selectByCode(dict.getCloumnCode()))) {
            return AjaxResult.error("栏目编码有误");
        }
        dict.setUpdateBy(ShiroUtils.getLoginName());
        SysTag tag = new SysTag();
        tag.setTagName(dict.getTagName());
        tag.setId(dict.getId());
        tag.setType(dict.getType());
        if(sysTagService.selectByNameAndType(tag) > 0){
            throw new RuntimeException("要修改的标签名称已经存在！");
        }
        return toAjax(sysTagService.updateTag(dict));
    }


}
